package com.weipeep.sys.controller;


import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.weipeep.common.base.BaseController;
import com.weipeep.sys.domain.ChannelDO;
import com.weipeep.sys.domain.SchoolDO;
import com.weipeep.sys.service.ChannelService;
import com.weipeep.sys.service.SchoolService;
import com.weipeep.sys.service.StaffService;
import com.weipeep.sys.vo.ConsultantVo;
import com.weipeep.sys.vo.StaffVo;
import com.weipeep.sys.vo.ValueTextVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weipeep.sys.domain.ConsultantDO;
import com.weipeep.sys.service.ConsultantService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-08-05 10:34:17 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/consultant")
public class ConsultantController extends BaseController {
	@Autowired
	private ConsultantService consultantService;
	@Autowired
	private ChannelService channelService;
	@Autowired
	private SchoolService schoolService;
	@Autowired
	private StaffService staffService;
	@GetMapping()
	@RequiresPermissions("sys:consultant:consultant")
	String Consultant(){
	    return "sys/consultant/consultant";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:consultant:consultant")
	public Result<Page<ConsultantVo>> list(Integer pageNumber, Integer pageSize, ConsultantVo consultantDTO){
		// 查询列表数据
		Page<ConsultantVo> page = new Page<>(pageNumber, pageSize);
        Wrapper<ConsultantVo> wrapper = new QueryWrapper<>(consultantDTO);
        page = consultantService.selectConsultantPage(page, wrapper);
		page.getRecords().forEach(consultantVo -> {
			consultantVo.setContactMethodTitle(fetchContactMethodTitle(consultantVo.getContactMethod()));
			consultantVo.setFollowStepTitle(fetchFollowStepTitle(consultantVo.getFollowStep()));
			consultantVo.setGradeTitle(fetchGradeTitle(consultantVo.getGrade()));
			consultantVo.setIntentionLevelTitle(fetchIntentionLevelTitle(consultantVo.getIntentionLevel()));
		});
        int total = consultantService.selectConsultantCount(wrapper);
		page.setTotal(total);
        return Result.ok(page);
	}

	@GetMapping("/add")
	@RequiresPermissions("sys:consultant:add")
	String add(Model model){
		initData(model);
	    return "sys/consultant/add";
	}

	private String fetchNationTitle(Integer nation){
		String nationTitle="";
		switch (nation){
			case 1:
				nationTitle = "汉族";
				break;
			case 2:
				nationTitle = "回族";
				break;
			case 3:
				nationTitle = "其他";
				break;
			default:
				nationTitle = "";
				break;
		}
		return nationTitle;
	}
	private String fetchContactMethodTitle(Integer contactMethod){
		String contactMethodTitle="";
		switch (contactMethod){
			case 1:
				contactMethodTitle = "来电";
				break;
			case 2:
				contactMethodTitle = "来访";
				break;
			case 3:
				contactMethodTitle = "网络";
				break;
			case 4:
				contactMethodTitle = "其他";
				break;
			default:
				contactMethodTitle = "";
				break;
		}
		return contactMethodTitle;
	}
	private String fetchIntentionLevelTitle(Integer intentionLevel) {
		String intentionLevelTitle = "";
		switch (intentionLevel) {
			case 1:
				intentionLevelTitle = "无意向";
				break;
			case 2:
				intentionLevelTitle = "有意向";
				break;
			case 3:
				intentionLevelTitle = "特别想来";
				break;
			case 4:
				intentionLevelTitle = "确定想来";
				break;
			default:
				intentionLevelTitle = "";
				break;
		}
		return intentionLevelTitle;
	}
	private String fetchFollowStepTitle(Integer followStep) {
		String followStepTitle = "";
		switch (followStep) {
			case 1:
				followStepTitle = "无意向";
				break;
			case 2:
				followStepTitle = "有意向";
				break;
			case 3:
				followStepTitle = "特别想来";
				break;
			case 4:
				followStepTitle = "确定想来";
				break;
			default:
				followStepTitle = "";
				break;
		}
		return followStepTitle;
	}
	private String fetchGradeTitle(Integer grade){
		String gradeTitle="";
		switch (grade){
			case 1:
				gradeTitle = "小班";
				break;
			case 2:
				gradeTitle = "中班";
				break;
			case 3:
				gradeTitle = "大班";
				break;
			case 4:
				gradeTitle = "一年级";
				break;
			case 5:
				gradeTitle = "二年级";
				break;
			case 6:
				gradeTitle = "三年级";
				break;
			case 7:
				gradeTitle = "四年级";
				break;
			case 8:
				gradeTitle = "五年级";
				break;
			case 9:
				gradeTitle = "六年级";
				break;
			case 10:
				gradeTitle = "七年级(初一)";
				break;
			case 11:
				gradeTitle = "八年级(初二)";
				break;
			case 12:
				gradeTitle = "九年级(初三)";
				break;
			case 13:
				gradeTitle = "十年级(高一)";
				break;
			case 14:
				gradeTitle = "十一年级(高二)";
				break;
			case 15:
				gradeTitle = "十二年级(高三)";
				break;
			default:
				gradeTitle = "";
				break;
		}
		return gradeTitle;
	}

	/**
	 * 初始化数据
	 * @param model
	 */
	private void initData(Model model) {
		List<ValueTextVo> gradeDicList = Lists.newArrayList();
		gradeDicList.add(new ValueTextVo(1,"小班"));
		gradeDicList.add(new ValueTextVo(2,"中班"));
		gradeDicList.add(new ValueTextVo(3,"大班"));
		gradeDicList.add(new ValueTextVo(4,"一年级"));
		gradeDicList.add(new ValueTextVo(5,"二年级"));
		gradeDicList.add(new ValueTextVo(6,"三年级"));
		gradeDicList.add(new ValueTextVo(7,"四年级"));
		gradeDicList.add(new ValueTextVo(8,"五年级"));
		gradeDicList.add(new ValueTextVo(9,"六年级"));
		gradeDicList.add(new ValueTextVo(10,"七年级(初一)"));
		gradeDicList.add(new ValueTextVo(11,"八年级(初二)"));
		gradeDicList.add(new ValueTextVo(12,"九年级(初三)"));
		gradeDicList.add(new ValueTextVo(13,"十年级(高一)"));
		gradeDicList.add(new ValueTextVo(14,"十一年级(高二)"));
		gradeDicList.add(new ValueTextVo(15,"十二年级(高三)"));
		List<ValueTextVo> nationDicList = Lists.newArrayList();
		nationDicList.add(new ValueTextVo(1,"汉族"));
		nationDicList.add(new ValueTextVo(2,"回族"));
		nationDicList.add(new ValueTextVo(3,"其他族"));
		List<ValueTextVo> contactMethodDicList = Lists.newArrayList();
		contactMethodDicList.add(new ValueTextVo(1,"来电"));
		contactMethodDicList.add(new ValueTextVo(2,"来访"));
		contactMethodDicList.add(new ValueTextVo(3,"网络"));
		contactMethodDicList.add(new ValueTextVo(4,"其他"));
		List<ValueTextVo> intentionLevelDicList = Lists.newArrayList();
		intentionLevelDicList.add(new ValueTextVo(1,"无意向"));
		intentionLevelDicList.add(new ValueTextVo(2,"有意向"));
		intentionLevelDicList.add(new ValueTextVo(3,"特别想来"));
		intentionLevelDicList.add(new ValueTextVo(4,"确定要来"));
		List<ValueTextVo> followStepDicList = Lists.newArrayList();
		followStepDicList.add(new ValueTextVo(1,"待跟进"));
		followStepDicList.add(new ValueTextVo(2,"跟进中"));
		followStepDicList.add(new ValueTextVo(3,"已邀约"));
		followStepDicList.add(new ValueTextVo(4,"已到访"));
		followStepDicList.add(new ValueTextVo(5,"已失败"));
		List<ValueTextVo> channelIdDicList = Lists.newArrayList();
		Wrapper<ChannelDO> wrapper = new QueryWrapper<>();
		List<ChannelDO> channelDOList = channelService.list(wrapper);

		channelDOList.forEach(channelDO -> {
			ValueTextVo valueTextVo = new ValueTextVo();
			valueTextVo.setValue(channelDO.getId());
			valueTextVo.setTitle(channelDO.getTitle());
			channelIdDicList.add(valueTextVo);
		});
		List<ValueTextVo> recordIdDicList = Lists.newArrayList();
		List<StaffVo> staffVoList = staffService.selectStaffList(new StaffVo());
		staffVoList.forEach(staffVo -> {
			ValueTextVo valueTextVo = new ValueTextVo();
			valueTextVo.setValue(staffVo.getUserId());
			valueTextVo.setTitle(staffVo.getRealName());
			recordIdDicList.add(valueTextVo);
		});
		List<ValueTextVo> consultantSchoolDicList = Lists.newArrayList();
		Wrapper<SchoolDO> schoolWrapper = new QueryWrapper<SchoolDO>();
		List<SchoolDO> schoolDOList = schoolService.list(schoolWrapper);
		schoolDOList.forEach(schoolDO -> {
			ValueTextVo valueTextVo = new ValueTextVo();
			valueTextVo.setValue(schoolDO.getId());
			valueTextVo.setTitle(schoolDO.getTitle());
			consultantSchoolDicList.add(valueTextVo);
		});
		model.addAttribute("gradeDicList",gradeDicList);
		model.addAttribute("nationDicList",nationDicList);
		model.addAttribute("contactMethodDicList",contactMethodDicList);
		model.addAttribute("intentionLevelDicList",intentionLevelDicList);
		model.addAttribute("followStepDicList",followStepDicList);
		model.addAttribute("channelIdDicList",channelIdDicList);
		model.addAttribute("recordIdDicList",recordIdDicList);
		model.addAttribute("consultantSchoolDicList",consultantSchoolDicList);

	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:consultant:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ConsultantDO consultant = consultantService.getById(id);
		model.addAttribute("consultant", consultant);
	    return "sys/consultant/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:consultant:add")
	public Result<String> save( ConsultantDO consultant){
		consultant.setCreateTime(Calendar.getInstance().getTime());
		if (consultant.getRecordId()==null){
			consultant.setRecordId(getUserId());
			consultant.setRecordName(getUsername());
		}
		if(consultantService.save(consultant)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:consultant:edit")
	public Result<String>  update( ConsultantDO consultant){
		consultant.setUpdateTime(Calendar.getInstance().getTime());
		if (consultant.getRecordId()==null){
			consultant.setRecordId(getUserId());
			consultant.setRecordName(getUsername());
		}
		consultantService.updateById(consultant);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:consultant:remove")
	public Result<String>  remove( Long id){
		if(consultantService.removeById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:consultant:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		consultantService.removeByIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
